package com.tjsinfo.tjsoa.check.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.jdbc.core.RowMapper;

import com.excel.util.manager.ReportManager;
import com.google.gson.Gson;
import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.check.vo.TCheckEmployee;
import com.tjsinfo.tjsoa.check.vo.TCheckLead;
import com.tjsinfo.tjsoa.check.vo.TCheckMain;
import com.tjsinfo.tjsoa.check.vo.TCheckMessage;



public class TCheckManageImpl implements TCheckManage {

	private IHibernateDao hibernateDao;
	private IQbcDao qbcDao;
	private ISpringDao springDao;
	
	public ISpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}
	
	/**
	 * 读取领导班子考核EXCEL表数据
	 * @param xmlURL xml文件路径
	 * @param filePath EXCEL文件路径
	 * @return 集合对象
	 */
	public Boolean loadCheckLead(String xmlURL,String filePath,TCheckMain tCheckMain){
		 
		 ReportManager rm = new ReportManager();
		 List list  =  null;
		 try {
			Integer mainId =	addTCheckMain(tCheckMain); 
			list  = 	rm.doReadBody(filePath, xmlURL);
			List<TCheckLead> checkLeads = buildCheckLead(list);
			addLeadCheck(checkLeads,mainId);
			return  true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	
		 
	}
	
	/**
	 * 增加考核
	 * @param checkLeads
	 * @return
	 */
	public Boolean addLeadCheck(List<TCheckLead> checkLeads,Integer mainId){
		try {
			for (TCheckLead tCheckLead : checkLeads) {
				tCheckLead.setMainId(mainId);
				hibernateDao.save(tCheckLead);
			}
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	public List<TCheckLead> buildCheckLead(List excelrows){
		List<TCheckLead> checkLeads = new ArrayList<TCheckLead>();
    	Gson gson = new Gson();
    	for (Iterator iterator = excelrows.iterator(); iterator.hasNext();) {
			Map map = (Map) iterator.next();
			String jsonStr = gson.toJson(map);
			TCheckLead tCheckLead = gson.fromJson(jsonStr, TCheckLead.class);
			checkLeads.add(tCheckLead);
		}
    	return checkLeads;
	}

	public Boolean loadCheckEmployee(String xmlURL, String filePath,TCheckMain tCheckMain) {
		// TODO Auto-generated method stub
		ReportManager rm = new ReportManager();
		 List list  =  null;
		 try {
			Integer mainId =	addTCheckMain(tCheckMain); 
			list  = 	rm.doReadBody(filePath, xmlURL);
			List<TCheckEmployee> employees= buildCheckEmployee(list);
			addEmployeeCheck(employees,mainId);
			return  true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public Boolean addEmployeeCheck(List<TCheckEmployee> employees,Integer mainId) {
		// TODO Auto-generated method stub
		try {
			for (TCheckEmployee tCheckEmployee : employees) {
				tCheckEmployee.setMainId(mainId);
				hibernateDao.save(tCheckEmployee);
			}
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public List<TCheckEmployee> buildCheckEmployee(List excelrows) {
		// TODO Auto-generated method stub
		List<TCheckEmployee> checkEmployees = new ArrayList<TCheckEmployee>();
    	Gson gson = new Gson();
    	for (Iterator iterator = excelrows.iterator(); iterator.hasNext();) {
			Map map = (Map) iterator.next();
			String jsonStr = gson.toJson(map);
			TCheckEmployee tCheckEmployee = gson.fromJson(jsonStr, TCheckEmployee.class);
			checkEmployees.add(tCheckEmployee);
		}
    	return checkEmployees;
	}

	public List<TCheckMain> selectTCheckMain() {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TCheckMain.class);
		return qbcDao.getCriteria(criteria);
	}
	
	/**
	 * 添加考核评价
	 * @return
	 */
	public Integer addTCheckMain(TCheckMain tCheckMain){
		return 	hibernateDao.save(tCheckMain);
	}
	
	/**
	 * 领导班子汇总信息
	 * @param id
	 * @return
	 */
	public List<TCheckLead> checkleadmain(Integer id){
		StringBuffer sql = new StringBuffer("EXEC	dbo.leadchecksum @id = ? ");
		List<TCheckLead> leadList =  springDao.querySQL(sql.toString(), new Object[]{id},new RowMapper() {
			
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TCheckLead checkLead = new TCheckLead();
				checkLead.setPersonName(arg0.getString("personName"));
				checkLead.setZhibiao1(arg0.getDouble("zhibiao1"));
				checkLead.setZhibiao2(arg0.getDouble("zhibiao2"));
				checkLead.setZhibiao3(arg0.getDouble("zhibiao3"));
				checkLead.setZhibiao4(arg0.getDouble("zhibiao4"));
				checkLead.setZhibiao5(arg0.getDouble("zhibiao5"));
				checkLead.setZhibiao6(arg0.getDouble("zhibiao6"));
				checkLead.setZhibiao7(arg0.getDouble("zhibiao7"));
				checkLead.setZhibiao8(arg0.getDouble("zhibiao8"));
				checkLead.setZhibiao9(arg0.getDouble("zhibiao9"));
				checkLead.setZhibiao10(arg0.getDouble("zhibiao10"));
				checkLead.setZhibiao11(arg0.getDouble("zhibiao11"));
				checkLead.setZhibiao12(arg0.getDouble("zhibiao12"));
				checkLead.setZhibiao13(arg0.getDouble("zhibiao13"));
				checkLead.setSum(arg0.getDouble("mainsum"));
				return checkLead;
			}
		});
		return leadList;
	}
	
	/**
	 * 干部汇总信息
	 * @param id
	 * @return
	 */
	public List<TCheckEmployee> checkemployeemain(Integer id){
		StringBuffer sql = new StringBuffer("EXEC	dbo.employeechecksum @id = ? ");
		List<TCheckEmployee> leadList =  springDao.querySQL(sql.toString(), new Object[]{id},new RowMapper() {
			
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TCheckEmployee checkEmployee = new TCheckEmployee();
				checkEmployee.setPersonName(arg0.getString("personName"));
				checkEmployee.setZhibiao1(arg0.getDouble("zhibiao1"));
				checkEmployee.setZhibiao2(arg0.getDouble("zhibiao2"));
				checkEmployee.setZhibiao3(arg0.getDouble("zhibiao3"));
				checkEmployee.setZhibiao4(arg0.getDouble("zhibiao4"));
				checkEmployee.setZhibiao5(arg0.getDouble("zhibiao5"));
				checkEmployee.setZhibiao6(arg0.getDouble("zhibiao6"));
				checkEmployee.setZhibiao7(arg0.getDouble("zhibiao7"));
				checkEmployee.setZhibiao8(arg0.getDouble("zhibiao8"));
				checkEmployee.setZhibiao9(arg0.getDouble("zhibiao9"));
				checkEmployee.setZhibiao10(arg0.getDouble("zhibiao10"));
				checkEmployee.setZhibiao11(arg0.getDouble("zhibiao11"));
				checkEmployee.setSum(arg0.getDouble("mainsum"));
				return checkEmployee;
			}
		});
		return leadList;
	}
	
	
	/**
	 * 验证是否是为整数
	 * @param zhibiao
	 * @return
	 */
	private boolean isInt(Double zhibiao){
		if(zhibiao == null){
			return false;
		}
		Integer intzhibiao = zhibiao.intValue();
		if( zhibiao.equals(Double.valueOf(intzhibiao.toString()))  ){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 验证是否在一定范围内
	 * @param min 最小值
	 * @param max 最大值 
	 * @param zhibiao 
	 * @return
	 */
	private boolean doubleRang(Double min,Double max,Double zhibiao){
		if(zhibiao == null){
			return false;
		}
		if(zhibiao>=min&&zhibiao<=max){
			return true;
		}else{
			return false;
		}
	}
	
	private List<TCheckMessage> rowVerificationLead(TCheckLead checkLead,Integer rownumber){
		List<TCheckMessage> checkMessages = new ArrayList<TCheckMessage>();
		String message1 = "此数字不是整数有小数。";
		String message2 = "此数字超出范围。";
		TCheckMessage message = null;
		if(!isInt(checkLead.getZhibiao1())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行政治方向"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkLead.getZhibiao1())){
			message = new TCheckMessage();
			message.setMessage(((rownumber+3))+"行政治方向"+message2);
			checkMessages.add(message);
		}
		
		if(!isInt(checkLead.getZhibiao2())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行企业建设"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkLead.getZhibiao2())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行企业建设"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao3())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行社会责任"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkLead.getZhibiao3())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行社会责任"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao4())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行经济效益"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 20d, checkLead.getZhibiao4())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行经济效益"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao5())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行可持续发展"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 10d, checkLead.getZhibiao5())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行可持续发展"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao6())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行创新成效"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 10d, checkLead.getZhibiao6())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行创新成效"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao7())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行科学管理"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 10d, checkLead.getZhibiao7())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行科学管理"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao8())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行发扬民主"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkLead.getZhibiao8())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行发扬民主"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao9())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行整体合力"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkLead.getZhibiao9())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行整体合力"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao10())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行运行机制"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkLead.getZhibiao10())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行运行机制"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao11())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行联系群众"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkLead.getZhibiao11())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行联系群众"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao12())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行选人用人"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkLead.getZhibiao12())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行选人用人"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkLead.getZhibiao13())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行廉洁自律"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 10d, checkLead.getZhibiao13())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行廉洁自律"+message2);
			checkMessages.add(message);
		}
		return checkMessages;
	}
	
	private List<TCheckMessage> rowVerificationLead(TCheckEmployee checkEmployee,Integer rownumber){
		List<TCheckMessage> checkMessages = new ArrayList<TCheckMessage>();
		String message1 = "此数字不是整数有小数。";
		String message2 = "此数字超出范围。";
		TCheckMessage message = null;
		if(!isInt(checkEmployee.getZhibiao1())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行政治素质"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkEmployee.getZhibiao1())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行政治素质"+message2);
			checkMessages.add(message);
		}
		
		if(!isInt(checkEmployee.getZhibiao2())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行职业操守"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkEmployee.getZhibiao2())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行职业操守"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao3())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行作风建设"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkEmployee.getZhibiao3())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行作风建设"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao4())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行科学决策能力"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkEmployee.getZhibiao4())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行科学决策能力"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao5())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行推动执行能力"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkEmployee.getZhibiao5())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行推动执行能力"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao6())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行学习创新能力"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkEmployee.getZhibiao6())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行学习创新能力"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao7())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行团结建设能力"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkEmployee.getZhibiao7())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行团结建设能力"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao8())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行履职绩效（KPI关键绩效指标）"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 35d, checkEmployee.getZhibiao8())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行履职绩效（KPI关键绩效指标）"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao9())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行协同成效"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 15d, checkEmployee.getZhibiao9())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行协同成效"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao10())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行廉洁自律"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 10d, checkEmployee.getZhibiao10())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行廉洁自律"+message2);
			checkMessages.add(message);
		}
		if(!isInt(checkEmployee.getZhibiao11())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行执行党风廉政责任制"+message1);
			checkMessages.add(message);
		}
		if(!doubleRang(0d, 5d, checkEmployee.getZhibiao11())){
			message = new TCheckMessage();
			message.setMessage((rownumber+3)+"行执行党风廉政责任制"+message2);
			checkMessages.add(message);
		}
		
		return checkMessages;
	}
	
	
	/**
	 * 领导班子表格验证
	 * @param checkLeads
	 * @return
	 */
	
	public List<TCheckMessage> leadListVar(List<TCheckLead> checkLeads){
		List<TCheckMessage> checkMessages = new ArrayList<TCheckMessage>();
		Integer row = 1;
		for (TCheckLead tCheckLead : checkLeads) {
			checkMessages.addAll(rowVerificationLead(tCheckLead, row)) ;
			row++;
		}
		return checkMessages;
	}
	
	/**
	 * 领导干部表格验证
	 * @param employees
	 * @return
	 */
	public List<TCheckMessage> employeeListVar(List<TCheckEmployee> employees){
		List<TCheckMessage> checkMessages = new ArrayList<TCheckMessage>();
		Integer row = 1;
		for (TCheckEmployee tCheckEmployee : employees) {
			checkMessages.addAll(rowVerificationLead(tCheckEmployee, row)) ;
			row++;
		}
		return checkMessages;
	}
	
	/**
	 * 删除考核评测表
	 * @param id
	 * @return
	 */
	public Boolean deleteMain(Integer id){
		TCheckMain tCheckMain = (TCheckMain) hibernateDao.findById(TCheckMain.class, id);
		if ("领导班子".equals(tCheckMain.getType())) {
			StringBuffer sql = new StringBuffer("DELETE FROM dbo.t_check_lead where mainId = ? ");
			springDao.commonUpdate(sql.toString(), new Object[]{id});
			hibernateDao.delete(tCheckMain);
		} else {
			StringBuffer sql = new StringBuffer("DELETE FROM dbo.t_check_employee where mainId = ? ");
			springDao.commonUpdate(sql.toString(), new Object[]{id});
			hibernateDao.delete(tCheckMain);
		}
		return true;
	}
}
